package com.zwy.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class RedisHashUtil {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private JedisPool jedisPool;

    public void hset(Integer dbIndex, String key, String field, String value){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            jedis.hset(key, field, value);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public boolean hsetnx(Integer dbIndex, String key, String field, String value){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.hsetnx(key, field, value) == 1;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public List<String> hvals(Integer dbIndex, String key){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.hvals(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public void hmset(Integer dbIndex, String key, Map<String, String> fieldMap){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            jedis.hmset(key, fieldMap);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public String hget(Integer dbIndex, String key, String field){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.hget(key, field);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public List<String> hmget(Integer dbIndex, String key, String... fields){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.hmget(key, fields);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    public Map<String, String> hgetAll(Integer dbIndex, String key){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.hgetAll(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    /**
     * hash是否存在field
     * @param dbIndex
     * @param key
     * @param field
     * @return
     */
    public boolean hexists(Integer dbIndex, String key, String field){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.hexists(key, field);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    /**
     * hash 删除field
     * @param dbIndex
     * @param key
     * @param field
     * @return 成功删除的个数
     */
    public long hdel(Integer dbIndex, String key, String... field){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.hdel(key, field);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    /**
     * 返回hash的域 field
     * @param dbIndex
     * @param key
     * @return
     */
    public Set<String> hkeys(Integer dbIndex, String key){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.hkeys(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    /**
     * 返回hash 域的数量
     * @param dbIndex
     * @param key
     * @return
     */
    public long hlen(Integer dbIndex, String key){
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.select(dbIndex);
            return jedis.hlen(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

}
